package com.ly.pj.goods.dao;

import com.ly.pj.goods.pojo.Goods;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
 * 自定义GoodsDao实现类GoodsDaoImp，基于SqlSession对象
 * 实现与数据库会话(相当于加个Mapper注解，mapper注解是框架自动生成代理实现类)
 */
@Repository//持久层，描述类交给spring管理
public class GoodsDaoImp implements GoodsDao{
    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    public GoodsDaoImp(){
        System.out.println("GoodsDaoImp()");
    }

    @Override
    public List<Goods> findGoods() {
        //1.获取SqlSession对象
        SqlSession sqlSession=sqlSessionFactory.openSession();
        //2.基于SqlSession实现商品查询
        String statement="com.ly.pj.goods.dao.GoodsDao.findGoods";//namespace+elementId
        List<Goods> list=sqlSession.selectList(statement);
        //3.释放资源
        sqlSession.close();
        //4.返回结果
        return list;

    }
}
